/**********************************************************
 *
 *  Copyright (c) 2003  SeikoEpson Inc
 *  All Rights Reserved
 *
 *  File name : f_floor.c
 *  Function  :
 *        f_floor function returns a value whose fraction
 *        part is cleared.
 *        This file is copied from math.lib of 
 *                  CC33 tool(CC33v40.exe + CCPA45.exe, 
 *                  math of ansilib33v25 )
 *  original file's Revision  :
 *      2000/02/10    first release                 M.Igarashi
 *  Revision  :
 *      2003/ 3/11    IrumaSoft M.Takeishi   1.st design
 *
 **********************************************************/
#include <f_math.h>
#include <f_smcvals.h>

// ALGORITHM
// 1.separate x to fraction part and exponent part
//   if x<0 modf( -x, &x ) else modf( x, &x )
// 2.if modf( -x, &x ) is not 0 is round up
//


//
//  the architecture of float floating point
//
//   0 30         23 22                0  32 bit
//   -----------------------------------
//  | |   exponent  |  fraction         |
//   -----------------------------------
//
//  |               |                   |
//  |    8 bits     |    23 bits        |
//
//         bit    31         sign bit        (  1 bit  )
//             30 - 23      exponent part    (  8 bits )
//             22 -  0      fraction part    ( 23 bits )
//
//
//

float f_floor(float sfX){

	long lX;
	float sfX2,sfX3,sfRet;
	
	F_GETW(lX,sfX);		// get raw data
	
	if ( (lX&0x80000000) !=0x0ul){
		F_CLR_SIGN( sfX2,sfX );		// clear sign
		if(f_modf(sfX2,&sfX3) !=0.0f ){
			sfX3+=1.0f;			// -0.1 -> -1
		}
		F_SET_SIGN(sfRet,sfX3);	// set sign
	}else{
		f_modf(sfX,&sfRet);
	}

	return sfRet;
}
